package com.qqtech.qquan.idle.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.exception.OrderException;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.idle.dao.IdleBaseDao;
import com.qqtech.qquan.idle.model.IdleBase;
import com.qqtech.qquan.idle.model.IdleBaseVo;
import com.qqtech.qquan.order.enums.OrderBaseDeliveryStatusDictEnum;
import com.qqtech.qquan.order.enums.OrderBaseOrderStatusDictEnum;
import com.qqtech.qquan.order.enums.OrderBaseOrderTypeDictEnum;
import com.qqtech.qquan.order.enums.OrderBasePayStatusDictEnum;
import com.qqtech.qquan.order.enums.OrderBasePayTypeDictEnum;
import com.qqtech.qquan.order.enums.OrderBaseReturnStatusDictEnum;
import com.qqtech.qquan.order.enums.OrderDeliveryTypeDictEnum;
import com.qqtech.qquan.order.model.OrderBase;
import com.qqtech.qquan.order.model.OrderDelivery;
import com.qqtech.qquan.order.model.OrderItem;
import com.qqtech.qquan.order.service.OrderHandleService;
import com.qqtech.qquan.push.service.PushBizzService;
import com.qqtech.qquan.user.dao.UserAddressDao;
import com.qqtech.qquan.user.model.UserAddressVo;

@Service
public class IdleBaseServiceImpl extends BaseServiceImpl<IdleBase>implements IdleBaseService {
	public static final Logger log = LoggerFactory.getLogger(IdleBaseServiceImpl.class);

	@Resource
	private IdleBaseDao idleBaseDao;

	@Resource
	private UserAddressDao userAddressDao;

	@Resource
	private OrderHandleService orderHandleService;
	@Resource
	private PushBizzService pushBizzService;

	@Override
	protected BaseDao<IdleBase> getBaseDao() {
		return idleBaseDao;
	}

	@Override
	public IdleBaseVo handleDetail(Integer id) {
		idleBaseDao.updatePv(id);
		IdleBaseVo idleBaseVo = idleBaseDao.getById(id);
		return idleBaseVo;
	}

	@Override
	public Result handleOrder(Integer buyerId, Integer idleBaseId, Integer userAddressId) throws OrderException {
		Result result = new Result();
		int updateCount = idleBaseDao.updateMinusQuantity(idleBaseId);// 更新库存
		if (updateCount > 0) {
			IdleBaseVo idleBaseVo = idleBaseDao.getById(idleBaseId);
			UserAddressVo userAddress = userAddressDao.getById(userAddressId);
			if (userAddress != null) {
				if (userAddress.getCommunityId() == null) {
					result.addErr("地址信息不完整，请重新编辑您的地址");
				} else if (userAddress.getCommunityId().intValue() != idleBaseVo.getCommunityId().intValue()) {
					result.addErr("所选地址和物品不在同一个社区");
				}
			} else {
				result.addErr("地址不存在了");
			}
			if (!result.checkForm()) {
				return result;
			}
			// 订单
			OrderBase orderBase = new OrderBase();
			orderBase.setTotal(idleBaseVo.getPrice());
			// orderBase.setOrderNo("");
			orderBase.setTitle(idleBaseVo.getTitle());
			orderBase.setImg(idleBaseVo.getImg().split(";")[0]);
			orderBase.setOrderType(OrderBaseOrderTypeDictEnum.IDLE.getCode());
			orderBase.setBuyerId(buyerId);
			orderBase.setSellerId(idleBaseVo.getCreatorId());
			orderBase.setOrderStatus(OrderBaseOrderStatusDictEnum.UNCOMPLETED.getCode());
			orderBase.setOrderStatusAt(TimeUtil.now());
			orderBase.setPayType(OrderBasePayTypeDictEnum.ONLINE.getCode());
			orderBase.setPayStatus(OrderBasePayStatusDictEnum.UNPAYED.getCode());
			orderBase.setDeliveryStatus(OrderBaseDeliveryStatusDictEnum.UNSEND.getCode());
			orderBase.setDeliveryStatusAt(TimeUtil.now());
			orderBase.setReturnStatus(OrderBaseReturnStatusDictEnum.NONEED.getCode());
			orderBase.setReturnStatusAt(TimeUtil.now());
			orderBase.setIsCallBack(YesNoEnum.NO.getCode());
			orderBase.setAutoExpireAt(TimeUtil.addMins(TimeUtil.now(), 15)); // 未付款情况下自动失效时间  // 15分钟后
			orderBase.setCommunityId(userAddress.getCommunityId());
			
			List<OrderItem> orderItemList = new ArrayList<OrderItem>();
			OrderItem orderItem = new OrderItem();
			orderItem.setProductId(idleBaseVo.getId());
			orderItem.setName(idleBaseVo.getTitle());
			orderItem.setPrice(idleBaseVo.getPrice());
			orderItem.setQuntity(1);
			orderItem.setImg(idleBaseVo.getImg().split(";")[0]);
			orderItemList.add(orderItem);

			OrderDelivery orderDelivery = new OrderDelivery();
			orderDelivery.setType(OrderDeliveryTypeDictEnum.SELLERSEND.getCode());
			orderDelivery.setDeliveryName("");
			orderDelivery.setDeliveryNo("");
			orderDelivery.setHasNeedDelivery(YesNoEnum.NO.getCode());
			orderDelivery.setConsignee(userAddress.getContactName());
			orderDelivery.setPhone(userAddress.getContactPhone());
			orderDelivery.setAddress(userAddress.getAddress());
			result = orderHandleService.createOrder(orderBase, orderDelivery, orderItemList);
			pushBizzService.orderSelected((String) result.get("orderNo"));
		}
		return result;
	}

	@Override
	public int handleCancle(Integer id) {
		return idleBaseDao.updateAddQuantity(id);// 恢复库存;
	}

}